const express = require('express')
const router = express.Router()
const passport = require('../passport')
const paymentConfig = require('./paymentConfig')
const middleware = require('wechat-pay').middleware;
const orderService = require('services/order')
const templateMessageService = require('services/templateMessage')

router.use('/config', require('./config'))
router.use('/banner', require('./banner'))
router.use('/house', require('./house'))
router.use('/review', require('./review'))
router.use('/user', passport.auth, require('./user'))
router.use('/order', passport.auth, require('./order'))
router.use('/upload', passport.auth, require('./upload'))

router.use('/payment/wechat-notify', middleware(paymentConfig).getNotify().done(async function(message, req, res, next) {
  try {
    await orderService.handleNotify(message)
    const orderId = orderService.idFromMessage(message)
    res.reply('success')
    setTimeout(() => {
      templateMessageService.sendOrderSuccess(message.openid, orderId, message)
    }, 1000)
  } catch (e) {
    console.log('wechat notify error', e)
    res.reply(e)
  }
}))

module.exports = router

// const express = require('express')
// const router = express.Router()

// router.use('/house', require('./house'))
// router.use('/home', require('./home'))
// router.use('/city', require('./city'))
// module.exports = router